<?php if (Mage::helper('fbcp/config')->isEnable()) : ?>
    <div id="fb-root"></div>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script src="http://connect.facebook.net/en_US/all.js#appId=157605600977665&amp;xfbml=1"></script>
	<script>
    window.fbAsyncInit = function() {
        FB.init({
          appId  : <?php echo Mage::helper('fbcp/config')->getApi(); ?>,
          status : true,
          cookie : true,
          xfbml  : true
        });
    };
    </script>
	
	<script>
		jQuery.noConflict()
		jQuery("document").ready(function(){
			//if close button is clicked
			jQuery('#fbcoupon-close').click(function () {
				jQuery('#dialog').fadeOut(1000);
			});	
			//catch like event
			FB.Event.subscribe('edge.create', function(href, widget) {
				
				showWindowCoupon("dialog");
			});
			//catch unlike event
			FB.Event.subscribe('edge.remove', function(href, widget) {
				showWindowCoupon("dialog");
			});
		});
		
		function showWindowCoupon(id) {
			//Get the window height and width
			var winH = jQuery(window).height();
			var winW = jQuery(window).width();
				  
			//Set the popup window to center
			jQuery("#"+id).css('top',  winH/2-jQuery("#"+id).height()/2);
			jQuery("#"+id).css('left', winW/2-jQuery("#"+id).width()/2);
			
			//transition effect
			jQuery("#"+id).fadeIn(2000); 
			showCouponCode();
		}
		function showCouponCode() {
			jQuery.ajax({
				  url: "<?php echo Mage::getUrl('fbcp/index/index'); ?>",
				  dataType: 'json',
				  type : 'post',
				  data: "isAjax=1",
				  beforeSend: function(){
					jQuery('#ccode_placeholder').removeClass("amsocial-nocode");
					jQuery('#ccode_placeholder').html("Loading...");
				  },
				  success: function(data){	
						jQuery('#ccode_placeholder').html(data.coupon_code);
						jQuery("#coupon_code").val(data.coupon_code);
				  }
			});
		}
		function applyCoupon() {
			//window.location.href= "<?php echo Mage::getUrl('fbcp/index/coupon');?>" + "?coupon_code="+jQuery("#coupon_code").val();
			window.location.href = "<?php echo Mage::getUrl('checkout/cart/couponPost');?>" + "?coupon_code="+jQuery("#coupon_code").val();
		}
	</script>
	<style>
		.window {
			position:fixed;
			left:0;
			top:0;
			display:none;
			z-index:9999;
		}

		#dialog {
			border: 1px solid #DDDDDD;
			box-shadow: 0 0 5px #999999;
			background-color:#faa719;
			padding:3px;
			width:580px;
		}
		#dialog h3{
			color: #FFFFFF;
			font-size:22px;
			text-align:center;
			height:40px;
			padding-top:3px
		}
	</style>
		<?php $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount(); ?>
		<div id="dialog" class="window">
				<h3><?php echo Mage::helper('fbcp/config')->getTitle(); ?></h3>
				<div id="fbcp_content">
					<?php echo Mage::helper('fbcp/config')->getMessage(); ?>
					<p class="title_fbcp">Get Coupon code: </p>
					<div id="ccode_placeholder" class="amsocial-nocode">Code will appear here </div>
					
					<div class="amsocial-codecomment">Copy the code and use it in cart</div>
					<input type="hidden" value="" name="coupon_code" id="coupon_code" />
					<br/>
					<?php if ($cart > 0) :  ?>
						<button class="button" value="Apply Coupon" title="Apply Coupon" type="button" onclick="applyCoupon()">
							<span>
								<span>Apply This Coupon</span>
							</span>
						</button>
					<?php endif; ?>
				</div>
				
				<a id="fbcoupon-close" style="display: inline;"></a>
			
		</div>
    <div>
		<fb:like expr:href=' <?php echo Mage::helper('core/url')->getCurrentUrl(); ?> '/>
	</div>
    
<?php endif; ?>